package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.ItemCatMapper;
import com.jt.mapper.ItemMapper;
import com.jt.pojo.ItemCat;
import com.jt.pojo.User;
import com.jt.util.RedisUtil;
import com.jt.vo.PageResult;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class ItemCatServiceImpl  implements  ItemCatService {

    @Autowired
    private ItemCatMapper  itemCatMapper;

    @Autowired
    private  RedisUtil redisUtil;

    /**
     * 1.删除商品的分类的信息
     * 2.根据id与级别进行删除,如果级别为3,则只删除该分类,如果是1级或者2级,则
     * 需要把下面的分类也删除了
     */
    @Override
    public boolean deleteItemCat(Integer id,Integer level) {
        if (level==3){
            itemCatMapper.deleteById(id);
        } else if(level==2){
            QueryWrapper  queryWrapper=new QueryWrapper();
            queryWrapper.eq("parent_id", id);
            itemCatMapper.delete(queryWrapper);
        }else  if (level==1){
            QueryWrapper  queryWrapper=new QueryWrapper();
            queryWrapper.select("id");
            queryWrapper.eq("parent_id", id);
            //所有的二级菜单
            List two = itemCatMapper.selectList(queryWrapper);

            //删除所有的三级菜单
            for (Object o : two) {
                QueryWrapper  queryWrapper1=new QueryWrapper();
                queryWrapper1.eq("parent_id",o);
                itemCatMapper.delete(queryWrapper);
            }

            //删除二级菜单
            for (Object o : two) {
                QueryWrapper  three=new QueryWrapper();
                three.eq("id",o);
                itemCatMapper.delete(three);
            }
            //删除该一级菜单
            itemCatMapper.deleteById(id);
        }
        return true;
    }


    @Override
    public List<ItemCat> getItemCatList() {
       return itemCatMapper.selectList(null);
    }

    @Override
    public List<ItemCat> getByItemCatList(ItemCat itemCat) {
       return  itemCatMapper.selectList(new QueryWrapper<>(itemCat));
    }

    @Override
    public boolean updateItemCatByid(ItemCat itemCat) {
        int i = itemCatMapper.updateById(itemCat);
        return (i==1);
    }

    @Override
    public List<ItemCat> getAllFloorList() {
        List<ItemCat> itemCats = itemCatMapper.selectList(null);


        //获取一级
        List<ItemCat>  allItemCat=new ArrayList<ItemCat>();
        for (ItemCat itemCat : itemCats) {
            if (itemCat.getLevel()==1) {
                allItemCat.add(itemCat);
            }
        }

        //获取二级

        for (ItemCat itemCat : allItemCat) {
            Integer id = itemCat.getId();
            List<ItemCat>  two=new ArrayList<>();
            for (ItemCat cat : itemCats) {
                if (cat.getParentId()==id) {
                    two.add(cat);
                }
            }
            itemCat.setChildren(two);
        }

        //获取三级目录
        for (ItemCat itemCat : allItemCat) {
            List<ItemCat> children = itemCat.getChildren();
            for (ItemCat child : children) {
                Integer id = child.getId();
                List<ItemCat>  three=new ArrayList<>();
                for (ItemCat cat : itemCats) {
                    if (cat.getParentId()==id) {
                        three.add(cat);
                    }
                }
                child.setChildren(three);
            }
            return allItemCat;

        }
        return null;
    }

    @Override
    public List<ItemCat> findBytype(Integer type) {
        Map<Integer,List<ItemCat>> map = getMap();
        if(type == 1) return map.get(0);
        if(type == 2) return findTwoItemCatList(map);
        return findThreeItemCatList(map);

    }

    @Override
    public void save(ItemCat itemCat) {
        itemCatMapper.insert(itemCat);
    }

    //存储在redis中
    public List<ItemCat> getItemCatByRedis(ItemCat itemCat){
        long size = redisUtil.lGetListSize("itemCat");
        //判断redis中是否有数据
        if(size==0) {
            List<ItemCat> all = this.getItemCatList();
            for (Object u : all) {
                redisUtil.lSet("itemCat",u );
            }
        }

        List<Object> objs = redisUtil.lGet("itemCat",0,redisUtil.lGetListSize("itemCat"));
        List<ItemCat>  itemCats=new ArrayList<>();
        for (int i=0; i<objs.size() ;i++) {
            itemCats.add((ItemCat)objs.get(i));
        }

        Stream<ItemCat> stream = itemCats.stream();
        int level=itemCat.getLevel();

        System.out.println("筛选的级别为:"+level);
        Stream<ItemCat> itemCatStream = stream.filter((ItemCat itemCat0) -> {
            if (itemCat0.getLevel() == level)
                return true;
            return false;
        });

        return itemCatStream.collect(Collectors.toList());
    }


    public Map<Integer,List<ItemCat>> getMap(){
        Map<Integer,List<ItemCat>> map = new HashMap<>();
        //1.查询所有的数据
        List<ItemCat> itemCatList =
                itemCatMapper.selectList(null);
        //2.封装Map集合数据
        //2.1存储依据: 如果key存在?获取子级之后add操作
        //2.2         如果key不存在 则将key存储,同时将自己当做第一个元素保存
        for (ItemCat itemCat : itemCatList){
            int parentId = itemCat.getParentId();
            //判断map集合中是否有父级
            if(map.containsKey(parentId)){
                //有父级 将自己添加到子级中
                map.get(parentId).add(itemCat);
            }else{
                List<ItemCat> list = new ArrayList<>();
                list.add(itemCat);
                map.put(parentId,list);
            }
        }
        return map;
    }

    public List<ItemCat> findTwoItemCatList(Map<Integer,List<ItemCat>> map){
        //1.获取一级商品分类
        List<ItemCat> oneList = map.get(0);
        //2.根据一级查询二级
        for(ItemCat itemCat : oneList){
            List<ItemCat> twoList = map.get(itemCat.getId());
            itemCat.setChildren(twoList);
        }
        return oneList;
    }

    private List<ItemCat> findThreeItemCatList(Map<Integer,List<ItemCat>> map) {
        List<ItemCat> oneList = findTwoItemCatList(map);
        for (ItemCat itemCat : oneList){    //1级菜单
            if(itemCat.getChildren() !=null){
                for(ItemCat itemCat2 :itemCat.getChildren()){
                    List<ItemCat> threeList = map.get(itemCat2.getId());
                    itemCat2.setChildren(threeList);
                }
            }
        }
        return oneList;
    }
}
